<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="/common/quest.jsp"%>
<%!
	// 查询热门新闻,按照浏览数+点赞数+评论数排序
	String images( ) {
		Message msg = new Message();
		List<Map> list = QBuilder.db("basedb").queryHql("select new map(id as id, newsId as news_id, img as imgs)from News where hospital=999 and category=0");
		Map<String, List<String>> imgMap = new HashMap<>();
		List<String> imgsList = new ArrayList<>();
		for (Map newsMap:list) {
            Integer nid = (Integer)newsMap.get("id");
            List<Map> imgs = JSONUtil.fromJson(newsMap.get("imgs") + "", new TypeReference<List<Map>>(){});
            if (imgs != null) {
                for (Map img:imgs) {
					imgsList.add(Tools.getDesc(img.get("src")));
					addListKeyValue(imgMap, Tools.getDesc(img.get("pixel")), Tools.getDesc(img.get("src")));
                }
            }
        }
		Map result = new HashMap();
		Set<String> keySet = imgMap.keySet();
		result.put("count", keySet.size());
		result.put("pixels", keySet);
		result.put("imgs", imgsList);
		msg.put("data", result);
		return msg.toJson();
	}
	
	// 为Map<String, List<String>>某个键的值list增加一个元素
	void addListKeyValue(Map<String, List<String>> parent, String key, String url) {
		List<String> list = isEmpty(parent.get(key))?new ArrayList<String>():parent.get(key);
		list.add(url);
		parent.put(key, list);
	}
%>
<%
	Tools.encryptPrint(out, images());
%>
